//
//  SearchMIDIClubViewController.m
//  MIDI
//
//  Created by E.M.Game on 15/9/20.
//  Copyright (c) 2015年 QXSX. All rights reserved.
//

#import "SearchMIDIClubViewController.h"
#import "AppConfig.h"
#import "MIDISearchCell.h"
#import "CommFunc.h"
#import "APIAdress.h"
#import "SearchMIDIClubModel.h"
#import "QXNetWorkTool.h"
#import "MBProgressHUD+YC.h"
#import "MIDIClubViewCtrler.h"
#import "UIViewController+Create.h"
#import "UIViewController+NavItem.h"
#import "UIImageView+WebCache.h"

@interface SearchMIDIClubViewController ()<UITableViewDelegate , UITableViewDataSource , UISearchBarDelegate , QXNetWorkToolDelegate>
@property (weak, nonatomic) IBOutlet UITableView *myTableView;
@property (weak, nonatomic) IBOutlet UISearchBar *searchBar;
@property (strong , nonatomic) QXNetWorkTool *netTool;
@property (strong , nonatomic) NSMutableArray * dataArr;

@end

@implementation SearchMIDIClubViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.myTableView.dataSource = self;
    self.myTableView.delegate = self;
    self.myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.searchBar.delegate = self;
    self.netTool = [[QXNetWorkTool alloc]init];
    self.netTool.QXNetWorkDelegate = self;
    self.dataArr = [[NSMutableArray alloc]init];
    
    self.searchBar.layer.borderColor = RGBA(230, 232, 231, 1).CGColor;
    self.searchBar.layer.borderWidth = 1.0f;
    
    UITextField *txfSearchField = [self.searchBar valueForKey:@"_searchField"];
    txfSearchField.textColor = [UIColor grayColor];
    
    txfSearchField.layer.cornerRadius = 15.0f;
    txfSearchField.layer.masksToBounds = YES;
    txfSearchField.layer.borderColor = RGBA(219, 219, 219, 1).CGColor;
    txfSearchField.layer.borderWidth = 1.0f;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self configNavigationItem];
}


-(void) configNavigationItem
{
    [self.tabBarController configNavigationBarTintColor:UIColorRGBA(0, 0, 0, 1)];
    [self.navigationController.navigationBar setBarTintColor:MIDIColorOfNavigationBar];
    self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
    self.navigationItem.title = @"搜索迷笛CLUB";
    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor],UITextAttributeTextColor, [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:1.0],UITextAttributeTextShadowColor,[UIFont fontWithName:@"Hiragino Sans GB" size:18.0],UITextAttributeFont,nil]];
    [self configNavigationBackAction:^{
        
        [self.navigationController popViewControllerAnimated:YES];
    }];
}

-(void)pressLeft:(UIButton *)sender
{
    
    [self.navigationController popViewControllerAnimated:YES];
    NSLog(@"press left");
    
}

#pragma mark tableView datasource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArr.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat minHeight = 74;
    NSString * str = @"";
    CGFloat screenWidth = [UIScreen mainScreen].bounds.size.width;
    CGFloat maxTexWidth = screenWidth - 69 - 55;
    CGSize maxSize = CGSizeMake(maxTexWidth, 999);
    UIFont *font = [UIFont fontWithName:@"Hiragino Sans GB" size:10];
    CGSize textSize = [CommFunc sizeWithString:str font:font max:maxSize];
    CGFloat height = textSize.height + 43;
    return minHeight > height? minHeight : height;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MIDISearchCell * cell = nil;
    NSString * identifier = @"MIDIClubSearchCell";
    cell = [self.myTableView dequeueReusableCellWithIdentifier:identifier];
    if(cell == nil)
    {
        cell = [MIDISearchCell loadFromXib];
    }
    [self configCell:indexPath.row cell:cell];
    return cell;
}

-(void)configCell : (NSInteger)row cell:(MIDISearchCell *)mycell
{
    SearchMIDIClubModel * mdata = [self.dataArr objectAtIndex:row];
    [mycell.clubNameLabel setText:mdata.exam_site_name];
    [mycell.descLabel setText:mdata.address];
    
    if (![mdata.site_icon isEqualToString:@""])
    {
        [mycell.clubImageView sd_setImageWithURL:[NSURL URLWithString:mdata.site_icon]];
    }
}

#pragma mark - Table View Delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    SearchMIDIClubModel * mdata = [self.dataArr objectAtIndex:indexPath.row];
    if(mdata == nil)
        return;
    MIDIClubViewCtrler *vc = [MIDIClubViewCtrler create];
    [vc setSiteID:mdata.id];
    [self.navigationController pushViewController:vc animated:YES];
    
}

#pragma mark --------------------QxNetWorlTool----------------------
-(void)GetSuccessResponse:(id)Response andTag:(int)tag
{
    if(tag == 1)
    {
        [self.dataArr removeAllObjects];
        for(NSDictionary *dic in Response)
        {
            SearchMIDIClubModel * item = [SearchMIDIClubModel MyModelWithDict:dic];
            [self.dataArr addObject:item];
        }
        [self.myTableView reloadData];
    }
}

-(void)GetFailResponse:(id)Response andTag:(int)tag
{
    [MBProgressHUD showError:@"查询失败"];
}

#pragma mark -------------------searchBar------------------------

-(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
    [self.netTool GetDataFromServerWithPostMethodandInterfaceName:SearchMIDIClubInterface andParameter:@[@{@"site_name":searchBar.text}] tag:1];
    
    [self.searchBar resignFirstResponder];
}

-(void)searchBarTextDidEndEditing:(UISearchBar *)searchBar
{
    [self.netTool GetDataFromServerWithPostMethodandInterfaceName:SearchMIDIClubInterface andParameter:@[@{@"site_name":searchBar.text}] tag:1];
}




@end








